//go:build linux

package gohttp

import (
	"syscall"
	"time"
)

func NewConn(fd int32) *conn {
	return &conn{fd: fd, update_time: time.Now().Unix(), readed: false}
}

type conn struct {
	fd          int32
	update_time int64
	readed      bool
}

func (c *conn) Read(p []byte) (int, error) {
	if c.readed {
		return 0, nil
	}
	c.readed = true
	c.update_time = time.Now().Unix()
	return syscall.Read(int(c.fd), p)
}

func (c *conn) Release() {
	defer syscall.Close(int(c.fd))
	defer syscall.EpollCtl(int(c.fd), syscall.EPOLL_CTL_DEL, int(c.fd), nil)
}

func (c *conn) IsTimeout(timeout int64) bool {
	return time.Now().Unix()-c.update_time > timeout
}
